home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / sysdeps / mach / hurd / __setitmr.c < prev    next >
C/C++ Source or Header  |  1994-05-24  |  10KB  |  313 lines

  1. /* Copyright (C) 1994 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <ansidecl.h>
  20. #include <stddef.h>
  21. #include <errno.h>
  22. #include <sys/time.h>
  23. #include <hurd.h>
  24. #include <hurd/signal.h>
  25. #include <hurd/msg_request.h>
  26. #include <mach/message.h>
  27.  
  28. /* XXX Temporary cheezoid implementation of ITIMER_REAL/SIGALRM.  */
  29.  
  30. spin_lock_t _hurd_itimer_lock = SPIN_LOCK_INITIALIZER;
  31. struct itimerval _hurd_itimerval; /* Current state of the timer.  */
  32. mach_port_t _hurd_itimer_port;    /* Port the timer thread blocks on.  */
  33. thread_t _hurd_itimer_thread;    /* Thread waiting for timeout.  */
  34. int _hurd_itimer_thread_suspended; /* Nonzero if that thread is suspended.  */
  35. vm_address_t _hurd_itimer_thread_stack_base; /* Base of its stack.  */
  36. vm_address_t _hurd_itimer_thread_stack_size; /* Size of its stack.  */
  37. struct timeval _hurd_itimer_started; /* Time the thread started waiting.  */
  38.  
  39. static inline void
  40. subtract_timeval (struct timeval *from, const struct timeval *subtract)
  41. {
  42.   from->tv_usec -= subtract->tv_usec;
  43.   from->tv_sec -= subtract->tv_sec;
  44.   while (from->tv_usec < 0)
  45.     {
  46.       --from->tv_sec;
  47.       from->tv_usec += 1000000;
  48.     }
  49. }
  50.  
  51. /* Function run by the itimer thread.
  52.    This code must be very careful not ever to require a MiG reply port.  */
  53.  
  54. static void
  55. timer_thread (void)
  56. {
  57.   while (1)
  58.     {
  59.       error_t err;
  60.       /* The only message we ever expect to receive is the reply from the
  61.          signal thread to a sig_post call we did.  We never examine the
  62.      contents.  */
  63.       struct
  64.     {
  65.       mach_msg_header_t header;
  66.       error_t return_code;
  67.     } msg;
  68.  
  69.       /* Wait for a message on a port that noone sends to.  The purpose is
  70.      the receive timeout.  Notice interrupts so that if we are
  71.      thread_abort'd, we will loop around and fetch new values from
  72.      _hurd_itimerval.  */
  73.       err = __mach_msg (&msg.header,
  74.             MACH_RCV_MSG|MACH_RCV_TIMEOUT|MACH_RCV_INTERRUPT,
  75.             0, 0, _hurd_itimer_port,
  76.             _hurd_itimerval.it_value.tv_sec * 1000 +
  77.             _hurd_itimerval.it_value.tv_usec / 1000,
  78.             MACH_PORT_NULL);
  79.       switch (err)
  80.     {
  81.     case MACH_RCV_TIMED_OUT:
  82.       /* We got the expected timeout.  Send a message to the signal
  83.          thread to tell it to post a SIGALRM signal.  We use
  84.          _hurd_itimer_port as the reply port just so we will block until
  85.          the signal thread has frobnicated things to reload the itimer or
  86.          has terminated this thread.  */
  87.       __sig_post_request (_hurd_msgport,
  88.                   _hurd_itimer_port, MACH_MSG_TYPE_MAKE_SEND_ONCE,
  89.                   SIGALRM, __mach_task_self ());
  90.       break;
  91.  
  92.     case MACH_RCV_INTERRUPTED:
  93.       /* We were thread_abort'd.  This is to tell us that
  94.          _hurd_itimerval has changed and we need to reexamine it
  95.          and start waiting with the new timeout value.  */
  96.       break;
  97.  
  98.     case MACH_MSG_SUCCESS:
  99.       /* We got the reply message from the sig_post_request above.
  100.          Ignore it and reexamine the timer value.  */
  101.       __mach_msg_destroy (&msg.header); /* Just in case.  */
  102.       break;
  103.  
  104.     default:
  105.       /* Unexpected lossage.  Oh well, keep trying.  */
  106.       break;
  107.     }
  108.     }
  109. }
  110.  
  111. /* Forward declaration.  */
  112. static sighandler_t preempt_sigalrm (thread_t thread, int signo, int sigcode);
  113.  
  114. /* Called before any normal SIGALRM signal is delivered.
  115.    Reload the itimer, or disable the itimer.  */
  116.  
  117. static int
  118. setitimer_locked (const struct itimerval *new, struct itimerval *old,
  119.           void *crit)
  120. {
  121.   struct itimerval newval = *new;
  122.   struct timeval now, remaining, elapsed;
  123.   struct timeval old_interval;
  124.   error_t err;
  125.  
  126.   inline void kill_itimer_thread (void)
  127.     {
  128.       __thread_terminate (_hurd_itimer_thread);
  129.       __vm_deallocate (__mach_task_self (),
  130.                _hurd_itimer_thread_stack_base,
  131.                _hurd_itimer_thread_stack_size);
  132.       _hurd_itimer_thread = MACH_PORT_NULL;
  133.     }
  134.  
  135.   if ((newval.it_value.tv_sec | newval.it_value.tv_usec) != 0)
  136.     {
  137.       /* Make sure the itimer thread is set up.  */
  138.  
  139.       if (_hurd_signal_preempt[SIGALRM] == NULL)
  140.     {
  141.       static struct hurd_signal_preempt preempt =
  142.         { preempt_sigalrm, 0, 0, NULL };
  143.       _hurd_signal_preempt[SIGALRM] = &preempt;
  144.     }
  145.  
  146.       if (_hurd_itimer_port == MACH_PORT_NULL)
  147.     {
  148.       /* Allocate a receive right that the itimer thread will
  149.          block waiting for a message on.  */
  150.       if (err = __mach_port_allocate (__mach_task_self (),
  151.                       MACH_PORT_RIGHT_RECEIVE,
  152.                       &_hurd_itimer_port))
  153.         goto out;
  154.     }
  155.  
  156.       if (_hurd_itimer_thread == MACH_PORT_NULL)
  157.     {
  158.       /* Start up the itimer thread running `timer_thread' (below).  */
  159.       if (err = __thread_create (__mach_task_self (),
  160.                      &_hurd_itimer_thread))
  161.         return __hurd_fail (err);
  162.       _hurd_itimer_thread_stack_base = 0; /* Anywhere.  */
  163.       _hurd_itimer_thread_stack_size = __vm_page_size; /* Small stack.  */
  164.       if (err = __mach_setup_thread (__mach_task_self (),
  165.                      _hurd_itimer_thread,
  166.                      &timer_thread,
  167.                      &_hurd_itimer_thread_stack_base,
  168.                      &_hurd_itimer_thread_stack_size))
  169.         {
  170.           __thread_terminate (_hurd_itimer_thread);
  171.           _hurd_itimer_thread = MACH_PORT_NULL;      
  172.           goto out;
  173.         }
  174.       _hurd_itimer_thread_suspended = 1;
  175.     }
  176.     }
  177.  
  178.   if ((newval.it_value.tv_sec | newval.it_value.tv_usec) != 0 || old != NULL)
  179.     {
  180.       /* Calculate how much time is remaining for the pending alarm.  */
  181.       if (__gettimeofday (&now, NULL) < 0)
  182.     {
  183.       __spin_unlock (&_hurd_itimer_lock);
  184.       _hurd_critical_section_unlock (crit);
  185.       return -1;
  186.     }
  187.       elapsed = now;
  188.       subtract_timeval (&elapsed, &_hurd_itimer_started);
  189.       remaining = _hurd_itimerval.it_value;
  190.       if (timercmp (&remaining, &elapsed, <))
  191.     {
  192.       /* Hmm.  The timer should have just gone off, but has not been reset.
  193.          This is a possible timing glitch.  The alarm will signal soon. */
  194.       /* XXX wrong */
  195.       remaining.tv_sec = 0;
  196.       remaining.tv_usec = 0;
  197.     }
  198.       else
  199.     subtract_timeval (&remaining, &elapsed);
  200.  
  201.       /* Remember the old reload interval before changing it.  */
  202.       old_interval = _hurd_itimerval.it_interval;
  203.  
  204.       /* Record the starting time that the timer interval relates to.  */
  205.       _hurd_itimer_started = now;
  206.     }
  207.  
  208.   /* Load the new itimer value.  */
  209.   _hurd_itimerval = newval;
  210.  
  211.   if ((newval.it_value.tv_sec | newval.it_value.tv_usec) == 0)
  212.     {
  213.       /* Disable the itimer.  */
  214.       if (_hurd_itimer_thread && !_hurd_itimer_thread_suspended)
  215.     {
  216.       /* Suspend the itimer thread so it does nothing.  Then abort its
  217.          kernel context so that when the thread is resumed, mach_msg
  218.          will return to timer_thread (below) and it will fetch new
  219.          values from _hurd_itimerval.  */
  220.       if ((err = __thread_suspend (_hurd_itimer_thread)) ||
  221.           (err = __thread_abort (_hurd_itimer_thread)))
  222.         /* If we can't save it for later, nuke it.  */
  223.         kill_itimer_thread ();
  224.       else
  225.         _hurd_itimer_thread_suspended = 1;
  226.     }
  227.     }
  228.   /* See if the timeout changed.  If so, we must alert the itimer thread.  */
  229.   else if (remaining.tv_sec != new->it_value.tv_sec ||
  230.        remaining.tv_usec != new->it_value.tv_usec)
  231.     {
  232.       /* The timeout value is changing.  Tell the itimer thread to
  233.      reexamine it and start counting down.  If the itimer thread is
  234.      marked as suspended, either we just created it, or it was
  235.      suspended and thread_abort'd last time the itimer was disabled;
  236.      either way it will wake up and start waiting for the new timeout
  237.      value when we resume it.  If it is not suspended, the itimer
  238.      thread is waiting to deliver a pending alarm that we will override
  239.      (since it would come later than the new alarm being set);
  240.      thread_abort will make mach_msg return MACH_RCV_INTERRUPTED, so it
  241.      will loop around and use the new timeout value.  */
  242.       if (err = (_hurd_itimer_thread_suspended
  243.          ? __thread_resume : __thread_abort) (_hurd_itimer_thread))
  244.     {
  245.       kill_itimer_thread ();
  246.       goto out;
  247.     }
  248.       _hurd_itimer_thread_suspended = 0;
  249.     }
  250.  
  251.   __spin_unlock (&_hurd_itimer_lock);
  252.   _hurd_critical_section_unlock (crit);
  253.  
  254.   if (old != NULL)
  255.     {
  256.       old->it_value = remaining;
  257.       old->it_interval = old_interval;
  258.     }
  259.   return 0;
  260.  
  261.  out:
  262.   __spin_unlock (&_hurd_itimer_lock);
  263.   _hurd_critical_section_unlock (crit);
  264.   return __hurd_fail (err);
  265. }
  266.  
  267. /* Set the timer WHICH to *NEW.  If OLD is not NULL,
  268.    set *OLD to the old value of timer WHICH.
  269.    Returns 0 on success, -1 on errors.  */
  270. int
  271. DEFUN(__setitimer, (which, new, old),
  272.       enum __itimer_which which AND
  273.       struct itimerval *new AND struct itimerval *old)
  274. {
  275.   void *crit;
  276.  
  277.   switch (which)
  278.     {
  279.     default:
  280.       return __hurd_fail (EINVAL);
  281.  
  282.     case ITIMER_VIRTUAL:
  283.     case ITIMER_PROF:
  284.       return __hurd_fail (ENOSYS);
  285.  
  286.     case ITIMER_REAL:
  287.       break;
  288.     }
  289.  
  290.   crit = _hurd_critical_section_lock ();
  291.   __spin_lock (&_hurd_itimer_lock);
  292.   return setitimer_locked (new, old, crit);
  293. }
  294.  
  295. static sighandler_t
  296. preempt_sigalrm (thread_t thread, int signo, int sigcode)
  297. {
  298.   struct itimerval it;
  299.  
  300.   if (thread != _hurd_sigthread || signo != SIGALRM || sigcode != 0)
  301.     /* Too much monkey business.  */
  302.     return SIG_DFL;
  303.  
  304.   /* Either reload or disable the itimer.  */
  305.   __spin_lock (&_hurd_itimer_lock);
  306.   it = _hurd_itimerval;
  307.   it.it_value = it.it_interval;
  308.   setitimer_locked (&it, NULL, NULL);
  309.  
  310.   /* Continue with normal delivery of SIGALRM.  */
  311.   return SIG_DFL;
  312. }
  313.